package com.xinyuan.digit.next.subscriber.db.wapper;

import com.xinyuan.digit.next.subscriber.db.pojo.*;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * SQL执行条件封装
 *
 * @param <T>
 * @author liuchenghui 2025/10/23
 * @version 1.0
 */
public class LambdaQueryWrapper<T> implements Serializable {
    private final List<Condition> conditions = new ArrayList<>();
    private final OrderByBuilder orderByBuilder = new OrderByBuilder();
    private final Class<T> entityClass;

    public LambdaQueryWrapper(Class<T> entityClass) {
        this.entityClass = entityClass;
    }

    // ========== 条件查询方法 ==========

    // 等于
    public LambdaQueryWrapper<T> eq(SFunction<T, ?> column, Object value) {
        conditions.add(new Condition(column, Operator.EQ, value));
        return this;
    }

    // 不等于
    public LambdaQueryWrapper<T> ne(SFunction<T, ?> column, Object value) {
        conditions.add(new Condition(column, Operator.NE, value));
        return this;
    }

    // 大于
    public LambdaQueryWrapper<T> gt(SFunction<T, ?> column, Object value) {
        conditions.add(new Condition(column, Operator.GT, value));
        return this;
    }

    // 小于
    public LambdaQueryWrapper<T> lt(SFunction<T, ?> column, Object value) {
        conditions.add(new Condition(column, Operator.LT, value));
        return this;
    }

    // 大于等于
    public LambdaQueryWrapper<T> ge(SFunction<T, ?> column, Object value) {
        conditions.add(new Condition(column, Operator.GE, value));
        return this;
    }

    // 小于等于
    public LambdaQueryWrapper<T> le(SFunction<T, ?> column, Object value) {
        conditions.add(new Condition(column, Operator.LE, value));
        return this;
    }

    // 模糊查询
    public LambdaQueryWrapper<T> like(SFunction<T, ?> column, Object value) {
        conditions.add(new Condition(column, Operator.LIKE, value));
        return this;
    }

    // 左模糊查询
    public LambdaQueryWrapper<T> likeLeft(SFunction<T, ?> column, Object value) {
        conditions.add(new Condition(column, Operator.LIKE_LEFT, value));
        return this;
    }

    // 右模糊查询
    public LambdaQueryWrapper<T> likeRight(SFunction<T, ?> column, Object value) {
        conditions.add(new Condition(column, Operator.LIKE_RIGHT, value));
        return this;
    }

    // BETWEEN 查询
    public LambdaQueryWrapper<T> between(SFunction<T, ?> column, Object value1, Object value2) {
        conditions.add(new Condition(column, Operator.BETWEEN, new Object[]{value1, value2}));
        return this;
    }

    // IN 查询
    public LambdaQueryWrapper<T> in(SFunction<T, ?> column, Collection<?> values) {
        conditions.add(new Condition(column, Operator.IN, values));
        return this;
    }

    // IN 查询（数组）
    public LambdaQueryWrapper<T> in(SFunction<T, ?> column, Object... values) {
        conditions.add(new Condition(column, Operator.IN, values));
        return this;
    }

    // IS NULL
    public LambdaQueryWrapper<T> isNull(SFunction<T, ?> column) {
        conditions.add(new Condition(column, Operator.IS_NULL, null));
        return this;
    }

    // IS NOT NULL
    public LambdaQueryWrapper<T> isNotNull(SFunction<T, ?> column) {
        conditions.add(new Condition(column, Operator.IS_NOT_NULL, null));
        return this;
    }

    // ========== 排序方法（使用 OrderByBuilder） ==========

    /**
     * 添加升序排序字段
     */
    public LambdaQueryWrapper<T> orderByAsc(SFunction<T, ?> column) {
        orderByBuilder.asc(column);
        return this;
    }

    /**
     * 添加降序排序字段
     */
    public LambdaQueryWrapper<T> orderByDesc(SFunction<T, ?> column) {
        orderByBuilder.desc(column);
        return this;
    }

    /**
     * 批量添加升序排序字段
     */
    @SafeVarargs
    public final LambdaQueryWrapper<T> orderByAsc(SFunction<T, ?>... columns) {
        orderByBuilder.ascAll(columns);
        return this;
    }

    /**
     * 批量添加降序排序字段
     */
    @SafeVarargs
    public final LambdaQueryWrapper<T> orderByDesc(SFunction<T, ?>... columns) {
        orderByBuilder.descAll(columns);
        return this;
    }

    /**
     * 获取 OrderByBuilder 进行复杂排序配置
     */
    public OrderByBuilder orderBy() {
        return orderByBuilder;
    }

    /**
     * 清除所有排序条件
     */
    public LambdaQueryWrapper<T> clearOrder() {
        orderByBuilder.clear();
        return this;
    }

    // ========== Getter 方法 ==========

    public List<Condition> getConditions() {
        return conditions;
    }

    public List<OrderItem> getOrderItems() {
        return orderByBuilder.getOrderItems();
    }

    public Class<T> getEntityClass() {
        return entityClass;
    }

    // 判断是否有排序条件
    public boolean hasOrder() {
        return orderByBuilder.hasOrder();
    }

    // 判断是否有查询条件
    public boolean hasCondition() {
        return !conditions.isEmpty();
    }
}